﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using GestorDeFlotas.Core;

namespace GestorDeFlotasDesktop.AbmRol
{
    public partial class AbmRol : Form
    {
        public AbmRol()
        {
            InitializeComponent();
        }

        private void AbmRol_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'gD1C2012DataSet.TL_Roles' table. You can move, or remove it, as needed.
            this.tL_RolesTableAdapter.Fill(this.gD1C2012DataSet.TL_Roles);
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RefrescarDataGrid()
        {
            // Codigo para refrescar el DataGridView
            this.gD1C2012DataSet.GetChanges();
            this.tL_RolesTableAdapter.Fill(this.gD1C2012DataSet.TL_Roles);
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            DatosRol datosRol = new DatosRol(ModoAbm.Alta);
            if (datosRol.ShowDialog() == DialogResult.OK)
            {
                RefrescarDataGrid();
            }
        }

        private void grdRoles_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not on button cells. 
            if (e.RowIndex < 0 || (e.ColumnIndex !=
                grdRoles.Columns["rol_seleccionar"].Index && e.ColumnIndex != grdRoles.Columns["rol_eliminar"].Index)) return;

            // Retrieve the rol ID.
            Int32 rolID = (Int32)grdRoles["rol_codigo", e.RowIndex].Value;

            if (e.ColumnIndex == grdRoles.Columns["rol_seleccionar"].Index)
            {
                DatosRol datosRol = new DatosRol(ModoAbm.Modificacion, new Rol(rolID).Cargar());
                if (datosRol.ShowDialog() == DialogResult.OK)
                {
                    RefrescarDataGrid();
                }
            }
            else if (e.ColumnIndex == grdRoles.Columns["rol_eliminar"].Index)
            {
                if ((Boolean)grdRoles["rol_activo", e.RowIndex].Value)
                {
            	    new Rol(rolID).SetInactivo();
                    RefrescarDataGrid();
                }
            }            
        }
    }
}
